package com.jojo.bubble.service.impl;

import com.jojo.bubble.entity.Agent;
import com.jojo.bubble.entity.Dev;
import com.jojo.bubble.dao.DevDao;
import com.jojo.bubble.service.DevService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.util.List;

import javax.annotation.Resource;

/**
 * 设备表(Dev)表服务实现类
 *
 * @author jojo
 * @since 2023-07-21 04:47:29
 */
@Service("devService")
@Slf4j
public class DevServiceImpl implements DevService {
    @Resource
    private DevDao devDao;

    @Override
    public int count(Dev record) {
        return this.devDao.count(record);
    }

    @Override
    public int insert(Dev record) {
        return this.devDao.insert(record);
    }

    @Override
    public int update(Dev record) {
        return this.devDao.update(record);
    }

    @Override
    public int updateDevTypeByCode(Dev record) {
        return this.devDao.updateDevTypeByCode(record);
    }

    @Override
    public List<Dev> queryAllByLimit(Dev dev, Pageable pageable) {
        return this.devDao.queryAllByLimit(dev,pageable);
    }

    @Override
    public Dev getInfo(int id) {
        return this.devDao.queryById(id);
    }

    @Override
    public List<Dev> getList(Dev record) {
        return this.devDao.getList(record);
    }

    @Override
    public int delete(int id) {
        return this.devDao.delete(id);
    }

    @Override
    public int checkDev(Dev dev) {
        Dev record = devDao.queryByCode(dev.getCode());
        if (ObjectUtils.isEmpty(record)) {
            log.info("添加新设备：" + dev.getCode());
            devDao.insert(dev);
        } else {
            if (record.getStatus() == 0) {
                record.setStatus(1);
            }
            if (!StringUtils.hasLength(record.getName())) {
                record.setName(dev.getName());
            } else if (!record.getName().equals(dev.getName())){
                record.setStatus(2);
            } else if (record.getName().equals(dev.getName())) {
                record.setStatus(1);
            }
            record.setIntro(dev.getName());
            record.setSNum(dev.getSNum());
            record.setENum(dev.getENum());
            record.setPNum(dev.getPNum());
            devDao.update(record);
        }
        return 0;
    }

    @Override
    public Dev queryByCode(String code) {
        return devDao.queryByCode(code);
    }

    @Override
    public int cleanDev(String devCode) {
        Dev dev = devDao.queryByCode(devCode);
        if (!ObjectUtils.isEmpty(dev)) {
            dev.setStatus(0);
            devDao.update(dev);
        }
        return 0;
    }
}
